﻿using KYJControlLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;

namespace KYJScadaProModbusRtu
{
    public partial class FrmTapControl : Form
    {
        public FrmTapControl()
        {
            InitializeComponent();

            //开启定时器
            this.updateTimer = new Timer();
            this.updateTimer.Interval = 200;
            this.updateTimer.Tick += UpdateTimer_Tick;
            this.updateTimer.Start();

            //FormClosing事件关闭定时器
            this.FormClosing += (sender, e) =>
            {
                this.updateTimer.Stop();
            };
        }

        //实时更新的定时器
        private Timer updateTimer;

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            UpdateCommon(this);
        }

        #region 通用更新方法
        private void UpdateCommon(Control parentControl)
        {
            if (CommomMethods.plcDevice != null && CommomMethods.connectState)//设备连接状态
            {
                foreach (Control item in parentControl.Controls)
                {
                    //更新TextShow显示
                    if (item is DeviceControl deviceControl)
                    {
                        if (deviceControl.StateAddress.Length > 0)
                        {
                            byte value = 0;

                            if (byte.TryParse(CommomMethods.plcDevice[deviceControl.StateAddress]?.ToString(), out value))
                            {
                                deviceControl.DeviceState = value;
                            }
                        }
                    }
                    else if (item.HasChildren)//递归
                    {
                        UpdateCommon(item);
                    }
                }
            }
        }
        #endregion

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region 窗体拖动
        public static Point CPoint;

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            CPoint = new Point(-e.X, -e.Y);
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                this.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
            }
        }
        #endregion

        private void Common_DeviceControlEvent(object sender, EventArgs e)
        {
            if (CommomMethods.CommonWrite(sender.ToString(), "True").IsSuccess)
            {
                Thread.Sleep(100);
                if (CommomMethods.CommonWrite(sender.ToString(), "False").IsSuccess == false)
                {
                    CommomMethods.AddLog(true, "写入False失败：" + sender.ToString());
                }
            }
            else
            {
                CommomMethods.AddLog(true, "写入True失败：" + sender.ToString());
            }
        }

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
